<template>
  <nav class="navbar">
    <div class="navbar-container">
      <!-- Logo -->
      <div class="navbar-logo">
       <router-link to="/" class="logo-link"><img src="@/assets/images/shanhedaxuelogo.png" alt="学校Logo" class="logo-img"></router-link>
      </div>

      <!-- 导航链接区域 -->
      <ul class="nav-links">
        <li class="nav-item"><router-link to="/" class="nav-link">首页</router-link></li>
        <li class="nav-item"><router-link to="/about" class="nav-link">学院简介</router-link></li>
        <li><router-link to="/admission" class="nav-link">考试排名</router-link></li>
        <!-- <li class="nav-item dropdown" @mouseenter="showDropdown('organization')" @mouseleave="hideDropdown('organization', 300)">
          <router-link to="/adminDeptPage" class="nav-link">机构设置</router-link>
          <ul v-if="activeDropdown === 'organization'" class="dropdown-menu" @mouseenter="cancelHideDropdown" @mouseleave="hideDropdown('organization', 300)">
            <li><router-link to="/adminDept" class="dropdown-item">行政机构</router-link></li>
            <li><router-link to="/departmental" class="dropdown-item">院系设置</router-link></li>
          </ul>
        </li>
        <li class="nav-item dropdown" @mouseenter="showDropdown('education')" @mouseleave="hideDropdown('education', 300)">
          <router-link to="/talentTrainingPage" class="nav-link">人才培养</router-link>
          <ul v-if="activeDropdown === 'education'" class="dropdown-menu" @mouseenter="cancelHideDropdown" @mouseleave="hideDropdown('education', 300)">
            <li><router-link to="/learningPlan" class="dropdown-item">学习计划</router-link></li>
            <li><router-link to="/abilityExam" class="dropdown-item">能力测试</router-link></li>
            <li><router-link to="/certificateExams" class="dropdown-item">证书考试</router-link></li>
          </ul>
        </li>
        <li class="nav-item"><router-link to="/faculty" class="nav-link">师资力量</router-link></li>
        <li class="nav-item dropdown" @mouseenter="showDropdown('admissions')" @mouseleave="hideDropdown('admissions', 300)">
          <router-link to="/admissionCareerPage" class="nav-link dropdown-toggle">招生就业</router-link>
          <ul v-if="activeDropdown === 'admissions'" class="dropdown-menu" @mouseenter="cancelHideDropdown" @mouseleave="hideDropdown('admissions', 300)">
            <li><router-link to="/admission" class="dropdown-item">招生信息</router-link></li>
            <li><router-link to="/careerGuidance" class="dropdown-item">就业指导</router-link></li>
          </ul>
        </li>
        <li class="nav-item"><router-link to="/library" class="nav-link">图书馆</router-link></li> -->
      </ul>
      
      <!-- 用户区域(最右侧) -->
      <div class="user-area">
        <!-- 已登录状态显示头像和昵称 -->
        <div v-if="isLoggedIn" class="user-info">
          <img :src="userAvatar" alt="用户头像" class="user-avatar">
          <span class="user-nickname">{{ userName }}</span>
        </div>
        
        <!-- 未登录状态显示登录按钮 -->
        <button v-else class="login-btn">登录</button>
      </div>
    </div>
  </nav>
</template>

<script>
export default {
  data() {
    return {
      // 登录状态标识
      isLoggedIn: false,
      // 用户头像(登录后显示)
      userAvatar: '',
      // 用户名(登录后显示)
      userName: '',
      // 当前激活的下拉菜单
      activeDropdown: null,
      // 下拉菜单隐藏计时器
      dropdownHideTimer: null
    };
  },
  mounted() {
    // 实际项目中这里会从后端或本地存储获取用户状态
    // 示例: 检查本地存储中的用户信息
    const storedUser = localStorage.getItem('currentUser');
    if (storedUser) {
      const user = JSON.parse(storedUser);
      this.isLoggedIn = true;
      this.userAvatar = user.avatar || '@/assets/default-avatar.png';
      this.userName = user.name || '用户';
    }
  },
  methods: {
    // 显示下拉菜单
    showDropdown(dropdownName) {
      // 清除可能存在的隐藏计时器
      this.cancelHideDropdown();
      // 显示下拉菜单
      this.activeDropdown = dropdownName;
    },
    
    // 延迟隐藏下拉菜单
    hideDropdown(dropdownName, delay = 0) {
      // 清除可能存在的隐藏计时器
      this.cancelHideDropdown();
      
      // 设置新的延迟隐藏计时器
      this.dropdownHideTimer = setTimeout(() => {
        this.activeDropdown = null;
        this.dropdownHideTimer = null;
      }, delay);
    },
    
    // 取消隐藏下拉菜单
    cancelHideDropdown() {
      if (this.dropdownHideTimer) {
        clearTimeout(this.dropdownHideTimer);
        this.dropdownHideTimer = null;
      }
    }
  },
  // 组件销毁时清除计时器
  beforeUnmount() {
    this.cancelHideDropdown();
  }
};
</script>

<style scoped>
.navbar {
  position: fixed; /* 固定定位 */
  top: 0; /* 固定在顶部 */
  left: 0;
  right: 0;
  z-index: 999; /* 确保在其他内容上方显示 */
  background-color: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 0 20px;
}

.navbar-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 1200px;
  margin: 0 auto;
}

.navbar-logo .logo-img {
  height: 50px;
  width: auto;
}

.navbar-title {
  font-size: 22px;
  font-weight: bold;
  color: #333;
  margin-left: 15px;
}

.nav-links {
  display: flex;
  list-style: none;
  margin: 0 20px;
  padding: 0;
}

.nav-item {
  margin: 0 12px;
  transition: all 0.3s ease;
}

.nav-item:hover{
  background-color: #7ab3eb;
   /* 设置圆角（关键属性） */
  border-radius: 8px; /* 数值越大，圆角越明显 */
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.15);
}

.nav-item:hover .nav-link {
  color: white; /* 子元素文本变白（与背景对比） */
}

/* 下拉菜单容器样式 */
.dropdown {
  position: relative;
  display: inline-block;
}

.nav-link {
  text-decoration: none;
  color: #333;
  font-size: 15px;
  transition: color 0.3s;
  white-space: nowrap;
}


/* 下拉菜单样式 - 整体居中于父元素下方 */
.dropdown-menu {
  position: absolute;
  top: 130%; /* 垂直方向上紧跟在父元素下方 */
  left: 50%; /* 水平方向上移动到父元素的中间位置 */
  transform: translateX(-50%); /* 通过负的50%自身宽度，实现下拉菜单整体居中 */
  min-width: 100px;
  background-color: white;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  padding: 8px 0;
  margin: 0;
  list-style: none;
  z-index: 1000;
  text-align: center;
}

.dropdown-item {
  display: block;
  padding: 8px 16px;
  color: #333;
  text-decoration: none;
  font-size: 14px;
  transition: background-color 0.3s;
}

.dropdown-item:hover {
  background-color: #c2c0c0;
  color: #0066cc;
}

/* 其他样式保持不变 */
.user-area {
  display: flex;
  align-items: center;
}

.login-btn {
  background-color: #0066cc;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  margin-right: 8px;
}

.user-nickname {
  color: #333;
  font-size: 14px;
  margin-right: 10px;
}

/* 响应式调整 */
@media (max-width: 992px) {
  .navbar-title {
    font-size: 18px;
  }
  
  .nav-item {
    margin: 0 8px;
  }
  
  .nav-link {
    font-size: 14px;
  }
  
  /* 响应式调整下拉菜单位置 */
  .dropdown-menu {
    left: 0;
    transform: none;
    min-width: 140px;
  }
}
</style>